<?php
/**
* api.php
*
* Create on 2015-7-7
* Create by TT
*/
namespace haibao\app\business;

use \haibao\app\data\Oauth as OauthData;

class Oauth extends \haibao\app\business\Base {
	
	public static function checkToken($token){
		if(empty($token)){
			throw new BusinessException('token不正确或失效',self::DATA_ERROR_CODE_TOKEN);
		}
		$oauthData = new OauthData();
		$appInfoArr = $oauthData->getToken($token);
		if(empty($appInfoArr)){
			throw new BusinessException('token不正确或失效',self::DATA_ERROR_CODE_TOKEN);
		} elseif($appInfoArr['Token'] != $token){
			throw new BusinessException('token不正确或失效',self::DATA_ERROR_CODE_TOKEN);
		}
		
		return $appInfoArr;
	}
	
	public static function setToken($appInfo,$appId,$appType,$signature = ''){
		if(empty($appId) || !isset($appType)){
			throw new BusinessException('参数不正确',self::DATA_ERROR_CODE_EMPTY);
		}
		//验证appId与appSecret
		$oauthData = new OauthData();
		if(empty($appInfo) || empty($appInfo[$appType]) || empty($appInfo[$appType]['AppId']) || $appInfo[$appType]['AppId'] != $appId){
			throw new BusinessException('参数不正确',self::DATA_ERROR_CODE_EMPTY);
		}
		$appSecret = $appInfo[$appType]['AppSecret'];
		$oauthData = new OauthData();
		return $oauthData->setToken($appId,$appSecret,$appType,$signature);
	}
}